//
//  SCB_Today_Default_detail_ViewController.m
//  Time
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SCB_Today_Default_detail_ViewController.h"
#import "SCB_Today_Detail_Model.h"
#import "Today_Today_detailTableViewCell.h"

@interface SCB_Today_Default_detail_ViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
Today_Today_detailTableViewCellDelegate
>
/**
 *  评论人数
 */
@property (nonatomic,retain) UILabel *commentLabel;
/**
 *  收藏人数
 */
@property (nonatomic,retain) UILabel *likesLabel;
/**
 *  主页面
 */
@property (nonatomic,retain) UITableView *tableView;
/**
 *  标题大图
 */
@property (nonatomic,retain) UIImageView *titleImageView;
/**
 *  回传的cell高度
 */
@property (nonatomic,assign) CGFloat height;
@property (nonatomic,assign) CGFloat width;

@end

@implementation SCB_Today_Default_detail_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self createView];
    [self createToolView];
    [self handleDate];
}

-(void)handleDate{
    NSString *urlStr = @"http://api.pianke.me/version4.0/article/info.php?sig=59904FACD4F2FCC835F27F2F05DD8D22";
    NSString *body = [NSString stringWithFormat:@"contentid=%@&sized=1",self.contentId];
    NSDictionary *header = @{@"Authorization":HEADER};
    
    
    [SCBNetTool POST:urlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
        NSLog(@"数据抓取成功");
        NSDictionary *dictionary = result;
        NSDictionary *dic = [dictionary valueForKey:@"data"];
        self.model = [[SCB_Today_Detail_Model alloc] init];
        [self.model setValuesForKeysWithDictionary:dic];
        self.commentLabel.text = [NSString stringWithFormat:@"%@",self.model.comments];
        self.likesLabel.text = [NSString stringWithFormat:@"%@",self.model.likes];
        [self.titleImageView setImageWithURL:[NSURL URLWithString:self.model.coverimg] placeholderImage:nil];
        NSLog(@"id:%@",self.model.contentId);
        NSLog(@"111%@",self.model.coverimg);
        [self.tableView reloadData];
    } withFaolure:^(NSError *error) {
        NSLog(@"详细数据抓取失败%@",error);
    }];
    
}
/**
 *  基本视图创建
 */
-(void)createView{
    self.titleImageView = [[UIImageView alloc] init];
    [self.view addSubview:self.titleImageView];
    self.titleImageView.frame = CGRectMake(0, 0, SWIDTH, SHEIGHT * 0.3);
    self.titleImageView.backgroundColor = [UIColor cyanColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT - SWIDTH * 0.2) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[Today_Today_detailTableViewCell class] forCellReuseIdentifier:@"SCB_Today_Default_Detail_Cell"];
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT * 0.3)];
    titleView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = titleView;
    
}
/**
 *  工具条,用来 返回 收藏 分享 评论.
 */
-(void)createToolView{
    UIView *toolView = [[UIView alloc] init];
    [self.view addSubview:toolView];
    [toolView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
        make.height.mas_equalTo(SWIDTH * 0.2);
    }];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"向左"] forState:UIControlStateNormal];
    [backButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(toolView.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:shareButton];
    [shareButton setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    [shareButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(toolView.mas_right).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    [shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.commentLabel = [[UILabel alloc] init];
    [toolView addSubview:self.commentLabel];
    [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(shareButton.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    
    
    UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:commentButton];
    [commentButton setImage:[UIImage imageNamed:@"评论 (4).png"] forState:UIControlStateNormal];
    [commentButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.commentLabel.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    [commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.likesLabel = [[UILabel alloc] init];
    [toolView addSubview:self.likesLabel];
    [self.likesLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(commentButton.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    
    UIButton *collButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:collButton];
    [collButton setImage:[UIImage imageNamed:@"收藏 (3).png"] forState:UIControlStateNormal];
    [collButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likesLabel.mas_left).offset(0);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    [collButton addTarget:self action:@selector(collButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)backButtonAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(void)collButtonAction:(UIButton *)button{
    NSLog(@"收藏");
}

-(void)commentButtonAction:(UIButton *)button{
    NSLog(@"评论");
}

-(void)shareButtonAction:(UIButton *)button{
    NSLog(@"分享");
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Today_Today_detailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCB_Today_Default_Detail_Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor cyanColor];
    cell.html = self.model.html;
    cell.delegate = self;
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y < 0) {
        self.titleImageView.frame = CGRectMake((SWIDTH * (SHEIGHT * 0.3 - scrollView.contentOffset.y) / (SHEIGHT * 0.3) - SWIDTH) / -2, 0, SWIDTH * (SHEIGHT * 0.3 - scrollView.contentOffset.y) / (SHEIGHT * 0.3), SHEIGHT * 0.3 - scrollView.contentOffset.y);
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [Tools getTextHeight:self.model.html withWidth:SWIDTH - 20 withFontSize:17.0] + 600;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)changeCellHeight:(CGFloat)height width:(CGFloat)width{
    self.height = height;
    self.width = width;
//    self.tableView.rowHeight = self.height * self.width / SWIDTH;
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
